package com.sunsy.authserver.enums;

import com.sunsy.authserver.asert.IApiAssert;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.Arrays;

/**
 * api 错误码枚举
 *
 * @author 孙思远
 * @date 2021/7/19
 */
@Getter
@AllArgsConstructor
@ToString
public enum EApiErrorCode implements IApiAssert {
  SUCCESS(0, "执行成功"),
  FAILED(-1, "操作失败"),
  UNKNOWN(-2, "未知错误"),
  VALIDATE_EXC(-3, "校验异常"),
  DATA_NOT_FOUND(-4, "{0} 不存在"),
  PARAM_NOT_NULL(-5, "{0} 参数不能为空"),
  TENANT_ID_NOT_FOUNT(-6, "获取租户信息异常"),
  ;

  private long code;
  private String msg;

  public static EApiErrorCode fromCode(long code) {
    return Arrays.stream(EApiErrorCode.values()).filter(it -> code == it.getCode()).findFirst()
        .orElse(UNKNOWN);
  }
}
